// pages/confirmorder/confirmorder.js
var app = getApp();
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiIndex: [],
    valuetab: 2,
    currentBut: 0,
    isHide: true, //支付弹窗是否显示
    typePay: "wx_pay",
    buttonClicked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.personalInfo();
    that.addressInfo();
    var totalPrice = options.price * options.num;
    var goods_list = [{
      goods_id: options.goodsid,
      num: options.num
    }]
    that.setData({
      id: options.id,
      image: options.image,
      name: options.name,
      subtitle: options.subtitle,
      price: options.price,
      num: options.num,
      totalPrice: totalPrice.toFixed(2),
      finalTotalPrice: totalPrice.toFixed(2),
      goods_list: goods_list
    }, function() {
      that.get_freight(goods_list);
    })
  },
  onShow: function(e) {
    var that = this;
    that.personalInfo();
    that.addressInfo();
  },
  //获取运费
  get_freight: function(goodslist) {
    var that = this;
    utils.http("/api/v1/order/get_freight", {
      goods_list: goodslist
    }, function(res) {
      that.setData({
        freight: res.datas,
        finalTotalPrice: parseFloat(that.data.finalTotalPrice) + parseFloat(res.datas)
      })
    })
  },
  addressInfo: function() {
    var that = this;
    console.log(app.globalData.addressChid)
    if (app.globalData.addressChid != undefined) {
      utils.http("/api/v1/address/address_info", {
        id: app.globalData.addressChid
      }, function(res) {
        app.globalData.addressChid = undefined;
        that.setData({
          defaultAddress: res.datas,
          addressId: res.datas.id
        })
      })
    } else {
      utils.http("/api/v1/address/address_info", {}, function(res) {
        console.log(res);
        if (res.datas != null) {
          that.setData({
            defaultAddress: res.datas,
            addressId: res.datas.id
          })
        }

      })
    }

  },
  //我的个人信息
  personalInfo: function(e) {
    var that = this;
    utils.showLoading();
    utils.http("/api/v1/member/index", {}, function(res) {
      utils.hideLoading();
      that.setData({
        info: res.datas,
      })
    })

  },
  //关闭支付弹窗
  onColseTap: function(e) {
    this.setData({
      isHide: true
    })
  },
  //打开支付弹窗
  onOrderTap: function(e) {
    if (!this.data.addressId) {
      wx.showToast({
        title: '请填写地址~',
        icon: 'none'
      });
      return false;
    }
    this.setData({
      isHide: false
    })

  },
  //支付方式
  paymentTypeTap: function(e) {
    var id = e.currentTarget.dataset.id;
    var currentBut = id;
    this.setData({
      currentBut: currentBut,
      typePay: e.currentTarget.dataset.type,
      buttonClicked: false
    })
  },
  //立即支付
  onOrderSuccessTap: function(e) {
    var that = this;
    if (that.data.buttonClicked == false) {

      utils.showLoading();
      that.setData({
        buttonClicked: true
      })
      if (that.data.typePay == "wx_pay") {
        utils.http("/api/v1/Crowdgoods/add_order", {
          crowd_id: that.data.id,
          goods_list: that.data.goods_list,
          payment_code: that.data.typePay,
          address_id: that.data.addressId,
          get_type: 2
        }, function(res) {
          console.log(res);
          utils.hideLoading();
          if (res.state == 1) {
            wx.requestPayment({
              timeStamp: res.datas.timeStamp,
              nonceStr: res.datas.nonceStr,
              package: res.datas.package,
              signType: res.datas.signType,
              paySign: res.datas.paySign,
              success(res) {
                wx.redirectTo({
                  url: '../ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
                })
              },
              fail(res) {
                that.setData({
                  buttonClicked: false
                })
              }
            })
          } else if (res.state == 10001) {
            wx.redirectTo({
              url: '../ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
            })
          } else {
            utils.message(res.datas);
            that.setData({
              buttonClicked: false
            })
          }
        })
      } else if (that.data.typePay == "balance") {
        utils.http("/api/v1/Crowdgoods/add_order", {
          crowd_id: that.data.id,
          goods_list: that.data.goods_list,
          payment_code: that.data.typePay,
          address_id: that.data.addressId,
          get_type: 2
        }, function(res) {
          utils.hideLoading()
          if (res.state == 1) {
            wx.redirectTo({
              url: '../ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
            })
          } else if (res.state == 10001) {
            wx.redirectTo({
              url: '../ordersuccess/ordersuccess?orderid=' + res.datas.order_id + "&totalprice=" + res.datas.total_price
            })
          } else {
            utils.message(res.datas);
            that.setData({
              buttonClicked: false
            })
          }
        })
      }
    }


  },
  //跳入我的地址页面
  onMyAddressTap: function(e) {
    app.globalData.typeaddress = 0
    wx.navigateTo({
      url: '../personal/myaddress/myaddress?typeaddress=' + app.globalData.typeaddress,
    })
  },
})